package com.tm.media.controller;

import com.tm.media.model.entity.LoginEntity;
import com.tm.media.service.MediaService;
import com.tm.media.utils.utils.Result;
import com.tm.media.utils.utils.SendController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author Ly
 * @Data 2023/2/20 17:16
 * @ClassName Ssss
 */
@RestController
@RequestMapping("login")
public class Ssss {
    @Autowired
    private MediaService mediaService;
    @PostMapping("in")
    public Result in(@RequestBody LoginEntity login, HttpServletRequest request, HttpServletResponse response){
       return mediaService.in(login);
    }
    @PostMapping("zhuce")
    public Result zhuce (@RequestBody LoginEntity login){
        Integer flag= mediaService.zhuce(login);
        if (flag==0){
          return Result.FAIL(200,"注册成功");
        } else if (flag==1) {
            return Result.SUCCESS(205,"验证码已过期，请重新获取");
        }else if (flag==4){
            return Result.FAIL(201,"密码格式不正确");
        }else if (flag==5){
            return Result.FAIL(206,"验证码不能为空");
        }
      return Result.FAIL(203,"验证码输入不正确");
    }
    @PostMapping("yanzhengma")
    public Result yanzhengma(@RequestBody  LoginEntity login) throws Exception {
      return mediaService.yanzhengma(login.getEmail());
    }
    @PostMapping("forgetPassword")
    public Result forgetPassword(@RequestBody LoginEntity login) throws Exception {
        int flag = mediaService.forgetPassword(login);
        if (flag==0){
            return Result.FAIL(205,"邮箱格式错误");
        }else if (flag==1){
            return Result.SUCCESS(200,"请勿重复发送验证码");
        }else if (flag==2){
            return Result.FAIL(201,"邮箱不能为空");
        }
        return Result.FAIL(206,"邮箱发送成功");
    }
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody LoginEntity login) throws Exception {
        int flag= mediaService.updatePassword(login);
        if (flag==0){
            return Result.FAIL(200,"修改成功");
        } else if (flag==1) {
            return Result.SUCCESS(205,"验证码已过期，请重新获取");
        }else if (flag==4){
            return Result.FAIL(201,"密码格式不正确");
        }
        return Result.FAIL(203,"验证码输入不正确");
    }
@PostMapping("send")
    public String send(String phone){
    SendController sendController = new SendController();
   return sendController.LoginSendSms(phone);
}
}
